<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This class is representing a XML feed similar to this:
 * <author>
 *  <name>Tuomas Koski</name>
 *  <uri>http://lobstermonster.org/minime/</uri>
 * </author>
 * 
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Atom_Feed_Author implements Atom_ChildElement {
    
	/**
     * @var Array of Atom_ChildElement
     */
    private $childElements = array();
    
    const ELEMENT_AUTHOR = "author";
    
    public function addChildElement(Atom_ChildElement $childElement) {
        $this->childElements[] = $childElement;
    }
    
    /**
     * @return Atom_Feed_Author_Name
     */
    public function getName() {
        
        foreach ($this->childElements as $elm) {
            if($elm instanceof Atom_Feed_Author_Name) {
                return $elm;
            }
        }
        return null;
    }
    
    /**
     * @return Atom_Feed_Author_Uri
     */
    public function getUri() {
        
        foreach ($this->childElements as $elm) {
            if($elm instanceof Atom_Feed_Author_Uri) {
                return $elm;
            }
        }
        return null;
    }
    
	/**
     * This method should produce something like this:
     * 
     * <author>
     *  <uri>http://activitystrea.ms/schema/1.0/person</uri>
     *  <name>Tuomas Koski</name>
     * </author>
     * 
     * @param DOMDocument$dom
     */
    public function asDomElement(DOMDocument $dom) {
        $element = $dom->createElement(self::ELEMENT_AUTHOR);
        foreach ($this->childElements as $childElement) {
            $element->appendChild($childElement->asDomElement($dom));
        }
        return $element;
    }
    
    /**
     * @param DOMElement $elm
     * @return Atom_Feed_Author
     */
    public static function buildFromDOMElement(DOMElement $elm) {
        $author = new Atom_Feed_Author();
        $author->addChildElement(Atom_Feed_Author_Name::buildFromDOMElement($elm->getElementsByTagName(Atom_Feed_Author_Name::ELEMENT_NAME)->item(0)));
        $author->addChildElement(Atom_Feed_Author_Uri::buildFromDOMElement($elm->getElementsByTagName(Atom_Feed_Author_Uri::ELEMENT_URI)->item(0)));
        return $author;
    }
}